package com.wei.shopping.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wei.shopping.dao.UserMapper;
import com.wei.shopping.entrty.PageResult;
import com.wei.shopping.entrty.QueryPageBean;
import com.wei.shopping.entrty.Result;
import com.wei.shopping.pojo.Login;
import com.wei.shopping.pojo.Notices;
import com.wei.shopping.pojo.UserInfo;
import com.wei.shopping.pojo.UserRole;
import com.wei.shopping.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    /**
     * 通过用户名和手机号进行查找
     * @param username
     * @param phone
     * @return
     */
    @Override
    public UserInfo findByPhoneAndUsername(String username, String phone) {
        return userMapper.findByPhoneAndUsername(username,phone);
    }
    /**
     * 注册用户
     */
    @Override
    public void regist(UserInfo userInfo){
        userMapper.regist(userInfo);
    }

    /**
     * 登陆，通过用户名和密码
     * @param username
     * @param password
     * @return
     */
    @Override
    public UserInfo login(String username, String password) {
        return userMapper.login(username,password);
    }

    /**
     * 添加登陆信息
     * @param login
     */
    @Override
    public void addLogin(Login login){
        userMapper.addLogin(login);
    }

    /**
     * 添加用户权限
     * @param userRole
     */
    @Override
    public void addRole(UserRole userRole){
        userMapper.addRole(userRole);
    }

    /**
     * 查找全部用户,参数role ：1普通用户，2管理员
     */
    @Override
    public PageResult findall(Integer userstatus, QueryPageBean queryPageBean){
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<UserInfo> page = userMapper.findall(userstatus, queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 设置用户的状态为封禁状态
     * @param userid
     */
    @Override
    public void userBan(String userid){
        userMapper.userBan(userid);
    }
    /**
     * 解封用户
     * @param userid
     */
    public void relieve(String userid){
        userMapper.relieve(userid);
    }
    /**
     * 设为管理员
     * @param userid
     */
    @Override
    public void setadmin(String userid){
        userMapper.setadmin(userid);
    }

    /**
     * 通过用户id查询用户
     * @param userid
     * @return
     */
    @Override
    public UserInfo findById(String userid){
        UserInfo userInfo = userMapper.findById(userid);
        return userInfo;
    }

    /**
     * 修改用户信息
     * @param info
     */
    @Override
    public void editmessage(UserInfo info){
        userMapper.editmessage(info);
    }

    /**
     * 修改密码
     * @param map
     * @param userid
     * @return
     */
    @Override
    public Result changepass(Map map,String userid){
        UserInfo user = userMapper.findById(userid);
        if(user.getPassword().equals(map.get("oldpass"))){
            //正确，修改密码user_info密码和login中的密码
            String newpass = (String) map.get("newpass");
            userMapper.changepass(userid,newpass);
            String loginpass = new Md5Hash(newpass, "Campus-shops").toString();
            userMapper.changeloginpass(userid,loginpass);
            return new Result(true,"修改密码成功");
        }else{
            //不正确
            return new Result(false,"旧密码错误，请重新输入");
        }
    }

    /**
     * 查找通知消息
     * @param userid
     * @return
     */
    @Override
    public List<Notices> getNotice(String userid){
        List<Notices> notice = userMapper.getNotice(userid);
        return notice;
    }
}
